#/usr/bin/env/python
# -*- coding=UTF-8 -*-
import pymysql

class Mysql(object):
	"""docstring for Mysql"""
	def __init__(self):
		self.host = '127.0.0.1'
		self.username = 'root'
		self.password = ''
		self.port = 3306
		self.db = 'blog'
		self.charset = 'uft8'


	#建立数据库连接
	def connect(self):
		try:
			conn = pymysql.connect(self.host,self.username,self.password,self.db,self.port,self.charset)
			return conn
		except:
			print("Error(1100):pymysql connect fail")


	#查询
	def fetchAll(self,sql):
		try:
			con = self.connect()
			cursor = con.cursor(pymysql.cursor.DictCursor)
			selctRes = cursor.execute(sql)
			return cursor.fetchall()
		except:
			print('Error(1101): select fail')


	#关闭句柄
	def closeDB(self):
		con = self.connect()
		con.close()

if __name__ == '__main__':

    DbHandle = Mysql()
    insertSql = "select * from name"
    DbHandle.fetchAll(insertSql)
    DbHandle.closeDB()



